//
//  EditFriendRemarkNameViewController.swift
//  MT
//
//  Created by ooops on 2020/5/7.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import RxSwift
import RxCocoa
import YPNavigationBarTransition

class MTEditFriendRemarkNameViewController: BaseViewController {
    
    var user: JMSGUser! {
        didSet {
            contentTextField.text = user.noteName
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: 0xf4f4f4)
        self.title = "设置备注"
        // Do any additional setup after loading the view.
        view.addSubview(tipRemarkLabel)
        view.addSubview(bgView)
        bgView.addSubview(contentTextField)
        
        tipRemarkLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(20 + StatusBarHeight + NavigationBarHeight)
        }
        
        bgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(64)
            make.top.equalTo(tipRemarkLabel.snp.bottom).offset(3)
        }
        
        contentTextField.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.commitButton)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.cancelButton)
        
        contentTextField.rx.text.orEmpty.asObservable().subscribe(onNext: {[weak self] (text) in
            guard let weakSelf = self else { return }
            if text != weakSelf.user.noteName {
                weakSelf.commitButton.backgroundColor = UIColor(hex: 0xffc000)
                weakSelf.commitButton.isUserInteractionEnabled = true
            } else {
                weakSelf.commitButton.backgroundColor = UIColor.text.x9b9b9b
                weakSelf.commitButton.isUserInteractionEnabled = false
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    @objc private func commitUserNoteName() {
        guard let userNoteName = self.contentTextField.text else { return }
//        if userNoteName == user.noteName {
//            return
//        }
        self.contentTextField.resignFirstResponder()
        user.updateNoteName(userNoteName) {[weak self] (result, error) in
            guard let weakSelf = self else  { return }
            
            if error == nil {
                MBProgressHUD_JChat.show(text: "修改成功", view: weakSelf.view)
                DispatchQueue.main.after(1) {
                    weakSelf.dismiss(animated: true, completion: nil)
                }
            }
        }
    }
    
    @objc private func cancelEdit() {
        
        self.dismiss(animated: true, completion: nil)
    }
    
    lazy var cancelButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("取消", for: .normal)
        btn.addTarget(self, action: #selector(cancelEdit), for: .touchUpInside)
        btn.setTitleColor(UIColor(hex: 0x9b9b9b), for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(14)
        btn.frame = CGRect(x: 0, y: 0, width: 40, height: 34)
        return btn
    }()
    
    lazy var commitButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("完成", for: .normal)
        btn.addTarget(self, action: #selector(commitUserNoteName), for: .touchUpInside)
        btn.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        //btn.backgroundColor = UIColor.text.x9b9b9b
        //btn.layer.cornerRadius = 4
        //btn.layer.masksToBounds = true
        if let image: UIImage = UIImage.init(color: UIColor(hex: 0xffc000), size: CGSize(width: 60, height: 24))?.imageCornerRadius(12) {
            btn.setBackgroundImage(image, for: .normal)
            //btn.addTarget(self, action: #selector(uploadAvator), for: .touchUpInside)
        }
        btn.titleLabel?.font = PingFangSCMediumFont(14)
        btn.isUserInteractionEnabled = false
        //btn.frame = CGRect(x: 0, y: 0, width: 40, height: 34)
        return btn
    }()
    
    lazy var tipRemarkLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "备注名"
        return l
    }()
    
    lazy var leftView: UILabel = {
        let l = UILabel()
        l.text = "d"
        l.frame = CGRect(x: 0, y: 0, width: 10, height: 1)
        l.textColor = UIColor.clear
        return l
    }()
    
    lazy var contentTextField: UITextField = {
        let txf = UITextField()
        txf.textColor = UIColor.text.x1d1a1a
        txf.font = PingFangSCMediumFont(14)
        txf.leftView = leftView
        txf.leftViewMode = .always
        txf.backgroundColor = .white
        return txf
    }()
    
    lazy var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        return v
    }()
    

}

extension MTEditFriendRemarkNameViewController {
    
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [.backgroundStyleColor, .backgroundStyleOpaque]
    }
}
